package com.mvu.banana.common.client;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootLayoutPanel;

/**
 * Interface description...
 *
 * @author gantz
 */
public abstract class Page extends Composite implements SystemConstants {

  public void go() {
    PermissionDTO dto = new PermissionDTO();
    dto.authKey = Window.Location.getParameter(AUTH_KEY_PARAM);
    EvalService.App.getInstance().getValues(dto, new AsyncCallbackAdapter<PermissionDTO>() {
      @Override
      protected void afterUpdate(PermissionDTO dto) {
        if (canVisit(dto)) {
          init();
          String page = String.valueOf(Page.this.getClass().getName().hashCode());
          History.newItem(page);
          RootLayoutPanel.get().clear();
          RootLayoutPanel.get().add(Page.this);
        } else {
          new LoginPage().go();
        }
      }
    });
  }

  protected abstract void init();

  public boolean canVisit(PermissionDTO dto) {
    return dto.username != null;
  }
}
